import Vue from 'vue'
import Router from 'vue-router'
import index from '@/views/index/index'

//搜索
import search from '@/views/index/search'
import searchList from '@/views/index/search_list'

//买车
import buyIndex from '@/views/buy_car/index'
import buyDetail from '@/views/buy_car/detail'
import DetailMap from '@/views/buy_car/detail_map'

import buyTool from '@/views/buy_car/tool'
import buyPk from '@/views/buy_car/car_pk'
import Compare from '@/views/buy_car/compare'

//发布
import relCar from '@/views/release/index'
import carUpdate from '@/views/release/index_update'
import relUser from '@/views/release/user_index'

//服务
import service from '@/views/service/index'
import svrDetail from '@/views/service/detail'
import svrOrder from '@/views/service/order'

//文章
import message from '@/views/message/index'
import msgDetail from '@/views/message/detail'

//注册登陆
import Login from '@/views/public/login'
import Register from '@/views/public/register'
import publicSvr from '@/views/public/service'
import forgetPwd from '@/views/public/forget_password'

//用户中心
import User from '@/views/user/index'
import Invite from '@/views/user/invite'
import inviteMore from '@/views/user/invite_more'
import userCollect from '@/views/user/user_collect'
import userCollectCar from '@/views/user/user_collect_car'
import userCollectArticle from '@/views/user/user_collect_article'
import userOrder from '@/views/user/user_order'
import userBook from '@/views/user/user_book'
import userBookEdit from '@/views/user/user_book_edit'
import userBookList from '@/views/user/user_book_list'
import userMessage from '@/views/user/message'
import UserInfo from '@/views/user/user_info'
import UserInfoUpdate from '@/views/user/user_info_update'

Vue.use(Router)

const router = new Router({
    //mode: 'history',
    // scrollBehavior (to, from, savedPosition) {
    //     if (savedPosition) {
    //         return savedPosition
    //     } else {
    //         return {x: 0, y: 0}
    //     }
    // },
    routes: [
        {
            path: '/',
            name: 'index',
            component: index,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/search',
            name: 'search',
            component: search
        },
        {
            path: '/search_list',
            name: 'searchList',
            component: searchList,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/buy',
            name: 'buyIndex',
            component: buyIndex,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/detail',
            name: 'buyDetail',
            component: buyDetail,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/tool',
            name: 'buyTool',
            component: buyTool,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/toolmini',
            name: 'buyToolMini',
            meta: {
                keepAlive: true,
            },
            component: () => import("@/views/buy_car/tool_mini")
        },
        {
            path: '/pk',
            name: 'buyPk',
            component: buyPk
        },
        {
            path: '/compare',
            name: 'Compare',
            component: Compare,
            meta: {
                keepAlive: false,
            },
        },

        //发布
        {
            path: '/relcar',
            name: 'relCar',
            component: relCar,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/carupdate',
            name: 'carUpdate',
            component: carUpdate,
            meta: {
                keepAlive: true,
            },
        },

        {
            path: '/reluser',
            name: 'relUser',
            component: relUser,
            meta: {
                keepAlive: false,
            },
        },

        {
            path: '/service',
            name: 'service',
            component: service,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/svr_detail',
            name: 'svrDetail',
            component: svrDetail,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/svr_order',
            name: 'svrOrder',
            component: svrOrder,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/message',
            name: 'message',
            component: message,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/msg_detail',
            name: 'msgDetail',
            component: msgDetail,
            meta: {
                keepAlive: false,
            },
        },


        //登陆注册
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/register',
            name: 'Register',
            component: Register
        },
        {
            path: '/forget',
            name: 'forgetPwd',
            component: forgetPwd
        },
        {
            path: '/public_svr',
            name: 'publicSvr',
            component: publicSvr,
            meta: {
                keepAlive: false,
            },
        },

        //用户中心
        {
            path: '/user',
            name: 'User',
            component: User,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/invite',
            name: 'Invite',
            component: Invite
        },
        {
            path: '/invite_more',
            name: 'inviteMore',
            component: inviteMore
        },
        {
            path: '/user_collect',
            name: 'userCollect',
            component: userCollect,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user_collect_car',
            name: 'userCollectCar',
            component: userCollectCar,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user_collect_art',
            name: 'userCollectArticle',
            component: userCollectArticle,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/user_order',
            name: 'userOrder',
            component: userOrder,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user_book',
            name: 'userBook',
            component: userBook,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user_book_edit',
            name: 'userBookEdit',
            component: userBookEdit,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user_book_list',
            name: 'userBookList',
            component: userBookList,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user_message',
            name: 'userMessage',
            component: userMessage,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/user_info',
            name: 'UserInfo',
            component: UserInfo,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/user/info/update',
            name: 'UserInfoUpdate',
            component: UserInfoUpdate,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: '/detail/map',
            name: 'DetailMap',
            component: DetailMap,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: '/user/code',
            name: 'userCode',
            meta: {
                keepAlive: true,
            },
            component: () => import("@/views/user/user_code.vue")
        }

    ],
    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            from.meta.scrollTop = window.scrollY;

            if (from.meta.keepAlive) {
                from.meta.savedPosition = document.body.scrollTop;
            }
            let postition=to.meta.savedPosition ||0;

            if(postition<=0){
                postition=from.meta.scrollTop;
            }


            return { x: 0, y: postition}
        }
    }
})

const { back, replace } = router;
//
// const originalPush = Router.prototype.push
// Router.prototype.push = function push(location) {
//     return originalPush.call(this, location).catch(err => err)
// }

router.back = function(to, form) {
    this.isBack = true;
    back.call(router);
};
router.replace = function(...args) {
    this.isReplace = true;
    replace.call(router, ...args);
};
router.beforeEach((to, form, next) => {
    // if(form.meta.keepAlive==true){
    //     try{
    //
    //         console.log(window.history)
    //
    //         var json={time:new Date().getTime()};
    //         window.history.pushState(json,form.name,form.path);
    //     }catch (e) {
    //
    //     }
    //
    // }
    next();
});

export default router;